package com.moleq.dialog;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.CompoundButton.OnCheckedChangeListener;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.models.BottleSetting;
import com.moleq.mgpos.activities.R;
import com.moleq.util.MoleQUtil;

public class EnvironmentalFeesDialog extends Dialog implements OnResultListener {

	private OnResultListener mOnResultListener;
	private CheckBox cb_env_fees,cb_tax1,cb_tax2,cb_tax3,cb_tax4,cb_tax5,cb_tax6,cb_wic,cb_fs;
	private EditText et_fees;
	private Button btn_digit_ok,btn_digit_cancel;
	private Context mContext;
	private List<BottleSetting> listBottle;
	private BottleSetting mBottleSetting;
	private MessageDialog mMessageDialog;
	private int selectedRow = 0;
	
	public void onCloseWithReturn(Object obj){
		mOnResultListener.onReturnValue(0,obj);
		dismiss();
	}
	
//	public EnvironmentalFeesDialog(Context context) {
////		super(context,context);
//		constructor(context);
//		
//	}
	public EnvironmentalFeesDialog(Context context, Object base) {
		super(context,R.style.dialog2);
		constructor(context,base);
	}
	
	public void setSource(List<BottleSetting> listBottle,BottleSetting bottle,int row){
		this.listBottle = listBottle;
		this.mBottleSetting = bottle;
		this.selectedRow = row;
	}
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}

	public EnvironmentalFeesDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}
	
	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.ps_dialog_bottle_digits);
		this.setCancelable(false);
	
		cb_env_fees = (CheckBox) findViewById(R.id.cb_env_fees);
		cb_tax1 = (CheckBox) findViewById(R.id.cb_tax1);
		cb_tax2 = (CheckBox) findViewById(R.id.cb_tax2);
		cb_tax3 = (CheckBox) findViewById(R.id.cb_tax3);
		cb_tax4 = (CheckBox) findViewById(R.id.cb_tax4);
		cb_tax5 = (CheckBox) findViewById(R.id.cb_tax5);
		cb_tax6 = (CheckBox) findViewById(R.id.cb_tax6);
		cb_wic = (CheckBox) findViewById(R.id.cb_wic);
		cb_fs = (CheckBox) findViewById(R.id.cb_fs);
		et_fees = (EditText) findViewById(R.id.et_fees);
		btn_digit_ok = (Button) findViewById(R.id.btn_digit_ok);
		btn_digit_cancel = (Button) findViewById(R.id.btn_digit_cancel);
		btn_digit_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_digit_cancel.setOnClickListener(OnClickDialogWindowListener);
		cb_env_fees.setOnCheckedChangeListener(onCheckedChangeListener);
	}
	
	public void show(){
		super.show();	
		getDetail(mBottleSetting);
	}
	
	public void getDetail(BottleSetting bottle){
		
		 cb_env_fees.setChecked(bottle.isBo_envf_flag()); 
		 if (cb_env_fees.isChecked()) {
			 cb_tax1.setChecked(bottle.isBo_envf_tax1()); 
			 cb_tax2.setChecked(bottle.isBo_envf_tax2()); 
			 cb_tax3.setChecked(bottle.isBo_envf_tax3()); 
			 cb_tax4.setChecked(bottle.isBo_envf_tax4()); 
			 cb_tax5.setChecked(bottle.isBo_envf_tax5()); 
			 cb_tax6.setChecked(bottle.isBo_envf_tax6()); 
			 cb_wic.setChecked(bottle.isBo_envf_wic()); 
			 cb_fs.setChecked(bottle.isBo_envf_fs()); 
			 et_fees.setText(bottle.getBo_envf_amt() == 0?"":MoleQUtil.fixNumber(Double.valueOf(bottle.getBo_envf_amt())));
		}else{
			enablecontrol(false);
		}
	}
	
	public Button.OnClickListener OnClickDialogWindowListener = new Button.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_digit_ok:
				
				if(validation() == false) return;			

				if (cb_env_fees.isChecked()) {
					mBottleSetting.setBo_envf_flag(cb_env_fees.isChecked()?true:false);
					mBottleSetting.setBo_envf_tax1(cb_tax1.isChecked()?true:false);
					mBottleSetting.setBo_envf_tax2(cb_tax2.isChecked()?true:false);
					mBottleSetting.setBo_envf_tax3(cb_tax3.isChecked()?true:false);
					mBottleSetting.setBo_envf_tax4(cb_tax4.isChecked()?true:false);
					mBottleSetting.setBo_envf_tax5(cb_tax5.isChecked()?true:false);
					mBottleSetting.setBo_envf_tax6(cb_tax6.isChecked()?true:false);
					mBottleSetting.setBo_envf_wic(cb_wic.isChecked()?true:false);
					mBottleSetting.setBo_envf_fs(cb_fs.isChecked()?true:false);
					mBottleSetting.setBo_envf_amt(Double.valueOf(et_fees.getText().toString()));
				}else{
					mBottleSetting.setBo_envf_flag(false);
					mBottleSetting.setBo_envf_tax1(false);
					mBottleSetting.setBo_envf_tax2(false);
					mBottleSetting.setBo_envf_tax3(false);
					mBottleSetting.setBo_envf_tax4(false);
					mBottleSetting.setBo_envf_tax5(false);
					mBottleSetting.setBo_envf_tax6(false);
					mBottleSetting.setBo_envf_wic(false);
					mBottleSetting.setBo_envf_fs(false);
					mBottleSetting.setBo_envf_amt(0);
				}
				listBottle.set(selectedRow, mBottleSetting);
				listBottle.get(selectedRow).setEdit(true);
				
				onCloseWithReturn(listBottle);
				dismiss();
				break;
			case R.id.btn_digit_cancel:
				dismiss();
				break;
			}
		}
	};
	
	OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			if (!cb_env_fees.isChecked()) {
				enablecontrol(false);
			}else{
				enablecontrol(true);
			}
		}
	};
	
	public boolean validation(){
		boolean bo = true;
		
		String fee = et_fees.getText().toString().trim();
		if (cb_env_fees.isChecked() && et_fees.getText().toString().trim().equals("")) {
			
			ShowMessage("Fees can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			bo = false;
		}else if (cb_env_fees.isChecked() && et_fees.getText().toString().trim().equals("0")) {
			
			ShowMessage("Fees can not be zero.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			bo = false;
		}else if (cb_env_fees.isChecked() && Double.valueOf(fee) <= 0) {
			
			ShowMessage("Fees can not be zero.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			bo = false;
		}
		return bo;
	}
	
	public void enablecontrol(boolean enable) {
		if (!enable) {
			cb_tax1.setChecked(enable);
			cb_tax2.setChecked(enable);
			cb_tax3.setChecked(enable);
			cb_tax4.setChecked(enable);
			cb_tax5.setChecked(enable);
			cb_tax6.setChecked(enable);
			cb_wic.setChecked(enable);
			cb_fs.setChecked(enable);
			et_fees.setText("");
		} else {

		}
		cb_tax1.setEnabled(enable);
		cb_tax2.setEnabled(enable);
		cb_tax3.setEnabled(enable);
		cb_tax4.setEnabled(enable);
		cb_tax5.setEnabled(enable);
		cb_tax6.setEnabled(enable);
		cb_wic.setEnabled(enable);
		cb_fs.setEnabled(enable);
		et_fees.setEnabled(enable);
	}
	
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener(){

		public void onClickYes() {
			
		}

		public void onClickNo() {
			mMessageDialog.dismiss();
		}

		public void onClickOK() {
			mMessageDialog.dismiss();
		}
	};
	
	public void ShowMessage(String text,int type,int icon){
		mMessageDialog = new MessageDialog(mContext);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}

	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass,
			int viewId, String returnvalue) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onReturnValue(int what, Object obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onShowingMessageDlg(boolean isShowing) {
		// TODO Auto-generated method stub
		
	}
}
